package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * dao层和数据库相关
 */
public class UserImpl implements UserDao {
    /**
     * 根据用户名和密码查询
     * 返回结果要么有一条数据，要么为null
     *
     * @param userModel
     * @return
     */
    @Override
    public UserModel findByUsernameAndPassword(UserModel userModel) {

        String sql = "select * from user where " +
                "username='" + userModel.getUsername() + "' " +
                "and password='" + userModel.getPassword() + "'";
//第四阶段：JDBC连接数据库
        ResultSet resultSet = JDBC.query(sql);
        UserModel model = null;
        try {
            while (resultSet.next()) {
                model = new UserModel();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return model;
    }

    @Override
    public int add(UserModel userModel) {
        String sql = "insert into (username,password,sex) values(" +
                "'" + userModel.getUsername() + "'," +
                "'" + userModel.getPassword() + "'," +
                "'" + userModel.getSex() + "')";
        return JDBC.update(sql);
    }

    @Override
    public int update(UserModel userModel) {
        String sql = "update user set " +
                "username='" + userModel.getUsername() + "'," +
                "password='" + userModel.getPassword() + "' " +
                "where id=" + userModel.getId();
        return JDBC.update(sql);
    }

    @Override
    public int delete(UserModel userModel) {
        String sql = "delete from user where id=" + userModel.getId();
        return JDBC.update(sql);
    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {
        String sql = "select * from user";
        List<UserModel> list = new ArrayList<>();
        ResultSet resultSet = JDBC.query(sql);
        try {
            while (resultSet.next()) {
                UserModel model = new UserModel();
//                     getInt("id")  ,getString("username") 引号里面的值对应数据库的列名
                model.setId(resultSet.getInt("id"));
                model.setUsername(resultSet.getString("username"));
                model.setPhone(resultSet.getString("phone"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public UserModel findById(UserModel userModel) {
        String sql = "select * from user where id=" + userModel.getId();
        ResultSet resultSet = JDBC.query(sql);
        UserModel model = null;
        try {
            while (resultSet.next()) {
                model = new UserModel();
                model.setId(resultSet.getInt("id"));
                model.setUsername(resultSet.getString("username"));
                model.setPhone(resultSet.getString("phone"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;

    }

    @Override
    public int getCount(UserModel userModel) {
        String sql = "select count(*) count from user ";
        ResultSet resultSet = JDBC.query(sql);
        try {
            while (resultSet.next()) {
                return resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
